home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia 1995 April / Informatica Multimedia CD - Epimundo.iso / DOS / FILE_CHG / STRPLINE.ZIP / STRPLINE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-12  |  10.0 KB  |  363 lines

  1. /*
  2.    
  3.                                   STRPLINE
  4.                                 Version 1.12
  5.            Copyright (c) Bob Swift, 1988-1993.  All Rights Reserved.
  6.                    Compiled with the Borland C++ compiler.
  7.    
  8.    This program is designed to read an input text file and strip out all
  9.    lines beginning with one of a series of patterns given in a configuration
  10.    file.  The configuration file is a simple text file with each match
  11.    pattern on a separate line.  Any line in the configuration file that
  12.    begins with a caret (^) is considered to be a comment line and will not
  13.    be processed.  The program creates a backup file called filename.BAK
  14.    prior to beginning the stripping operation.  This file is left upon
  15.    completion of the program.
  16.  
  17.    The format for using this program is as follows:
  18.  
  19.    
  20.         STRPLINE <filename> [configfile]
  21.    
  22.    where <filename>   is the name of the text file to be stripped
  23.          [configfile] is the file containing the patters to match
  24.                       default is STRPLINE.CFG in the current directory
  25.    
  26.    
  27.    The text file to be stripped must be in the current directory.  The
  28.    configuration may be specified with a full drive and path.  If the
  29.    configuration file is not specified, it will default to STRPLINE.CFG
  30.    in the current directory.  You are allowed a maximum of 256 search
  31.    patterns of 40 characters maximum.
  32.    
  33.    The program will display a VERY brief set of instructions if it is called
  34.    without any arguments or if it encounters an error.  The following is a
  35.    list of the error codes returned by the program:
  36.    
  37.                   0 - No errors.  Normal termination.
  38.                   1 - Missing or extra command line arguments.
  39.                   2 - Unable to open the input file.
  40.                   3 - Unable to open the output file.
  41.                   4 - Problem writing to output file.
  42.                   5 - Problem closing input file.
  43.                   6 - Problem closing output file.
  44.                   7 - Input file not in current directory.
  45.                   8 - Unable to open configuration file.
  46.                   9 - Problem closing configuration file.
  47.                  10 - No patterns in configuration file to strip.
  48.                  11 - Unable to open backup file.
  49.    
  50.    When an error is encountered, the program will exit immediately and will
  51.    attempt to properly close all files.
  52.    
  53.    Although I have chosen to retain all rights to this program, you are free
  54.    to use it under the following conditions:
  55.    
  56.             - You realize that there is NO Warrantee of any sort.
  57.               It was tested pretty thoroughly here before release
  58.               but who knows what bugs may be lurking within.
  59.             
  60.             - You will not modify the code and release a new version
  61.               of the program.  I welcome suggestions for improvement
  62.               (especially when accompanied by code) but I make no
  63.               guarantee of future releases.
  64.             
  65.             - If you find the program useful, I ask that you do 
  66.               something to brighten somebody else's day.  Just 
  67.               exactly what, I will leave up to you.
  68.  
  69.    
  70.    You may freely distribute this program provided that you distribute only 
  71.    the complete archive which includes the STRPLINE.EXE, STRPLINE.C,
  72.    STRPLINE.CFG and STRPLINE.DOC.  In addition, You MUST NOT charge for the
  73.    program nor can you charge a copy fee over $4.00 (including the price of
  74.    the diskette).  
  75.  
  76.    
  77.                                                    Bob Swift (1:342/5)
  78.  
  79.  
  80. */
  81.  
  82. #include <stdio.h>
  83. #include <string.h> 
  84.  
  85. void exit(int ernum);
  86. void linetest();
  87. void helpscrn(int errornumber);
  88.  
  89. FILE *infile,*outfile;
  90. int flag,flag1,count;
  91. unsigned in_line,strp_line,out_line;
  92. char *flnames[3];
  93. char cfgfile[128];
  94. char inline[256];
  95. char linetst1[40];
  96. char lintest[256][40];
  97. char *line;
  98. char version[] = {"1.12"};  /* Current Version Number */
  99.  
  100. void main(int argc, char *argv[])
  101. {
  102. printf("\n\nSTRPLINE Version %s - Copyright (c) Bob Swift, 1988-1993\n",version);
  103.  
  104. if (argc > 3 || argc < 2)
  105.    helpscrn(1);
  106.  
  107. in_line = 0;
  108. out_line = 0;
  109. strp_line = 0;
  110.  
  111. flnames[1]=strupr(argv[1]);             /*  Input File  */
  112.  
  113. if (argc == 3)
  114.    flnames[2]=strupr(argv[2]);          /*  Configuration File  */
  115. else
  116.    {
  117.    strcpy(cfgfile,"STRPLINE.CFG");
  118.    flnames[2] = cfgfile;
  119.    }
  120.  
  121. /*  Check if input file in current directory  */
  122. if (strchr(flnames[1],'\\')!=NULL || strchr(flnames[1],':')!=NULL)
  123.    helpscrn(7);
  124.  
  125. /*  Check if configuration file exists  */
  126. infile=fopen(flnames[2],"r");
  127. if (infile == NULL)
  128.    helpscrn(8);
  129.  
  130. flag = 0;
  131. flag1 = 0;
  132. count = 0;
  133.  
  134. /*  Read configuration file and set up search patters  */
  135. while (flag1 < 256)
  136. {
  137. if (fgets(inline,256,infile) != NULL)
  138.   {
  139.   line = inline;
  140.   if (inline[0]!='^')
  141.     {
  142.     count++;
  143.     flag1 = count;
  144.     strcpy(lintest[count],inline);
  145.     }
  146.   }
  147. else
  148.   flag1 = 256;
  149. }
  150.  
  151. flag=fclose(infile);
  152. if (flag != 0)
  153.    helpscrn(9);
  154.  
  155. /*  Exit if no patterns to match  */
  156. if (count < 1)
  157.    helpscrn(10);
  158.  
  159. /*  Strip off carriage returns from patterns  */
  160. for (flag=1;flag<=count;flag++)
  161.    {
  162.    for (flag1=0;lintest[flag][flag1]!='\n'&&lintest[flag][flag1]!='\0';flag1++)
  163.       {
  164.       /*  NULL Line  */
  165.       }
  166.    lintest[flag][flag1]='\0';
  167.    }
  168.  
  169. /*  If input file has no extension finish it with a period  */
  170. if (strchr(flnames[1],'.')==NULL)
  171.    strcat(flnames[1],".");
  172.  
  173. /*  Check if input file exists  */
  174. infile=fopen(flnames[1],"r");
  175. if (infile == NULL)
  176.    helpscrn(2);
  177. flag=fclose(infile);
  178. if (flag != 0)
  179.    helpscrn(5);
  180.  
  181. /*  Build file name for backup file  */
  182. strcpy(inline,flnames[1]);
  183. for (flag1=0;inline[flag1]!='.';flag1++)
  184.    {
  185.    /*  NULL Line  */
  186.    }
  187. inline[flag1] = '\0';
  188. strcat(inline,".BAK");
  189. flnames[3] = inline;
  190.  
  191. /*  Exit if input file has an extension of BAK  */
  192. if (strcmp(flnames[1],flnames[3])==0)
  193.    helpscrn(11);
  194.  
  195. /*  Check for existing backup file and delete  */
  196. infile=fopen(flnames[3],"r");
  197. fclose(infile);
  198. if (infile != NULL)
  199.    {
  200.    flag=unlink(flnames[3]);
  201.    if (flag != 0)
  202.       helpscrn(11);
  203.    }
  204.  
  205. /*  Rename input file with extension of BAK  */
  206. if (rename(flnames[1],flnames[3]) != 0)
  207.    helpscrn(11);
  208.  
  209. /*  Open source file  */
  210. infile=fopen(flnames[3],"r");
  211. if (infile == NULL)
  212.    helpscrn(2);
  213.  
  214. /*  Open destination file  */
  215. outfile=fopen(flnames[1],"w");
  216. if (outfile == NULL)
  217.    helpscrn(3);
  218.  
  219. /*  Print out list of patterns to match  */
  220. strcpy(inline,"\nStripping: ");
  221. for (flag=1;flag<=count;flag++)
  222.    {
  223.    if (lintest[flag][0]=='\0')
  224.      {
  225.      printf("%sBlank Lines\n",inline);
  226.      }
  227.    else
  228.      {
  229.      printf("%s%s\n",inline,lintest[flag]);
  230.      }
  231.    strcpy(inline,"           ");
  232.    }
  233.  
  234. /*  Here's where we actually do the stripping.  First print out a       */
  235. /*  heading for the on-screen line counter and initialize the counter.  */
  236. /*  printf("\n    Lines of Text:");  */
  237. printf("\n Input    Out   Stripped\n");
  238.  
  239. /*  Get line from source file and increment counter  */
  240. while (fgets(inline,256,infile) != NULL)
  241.   {
  242.   in_line++;
  243.   line = inline;
  244.  
  245. /*  Cycle through the search patterns  */
  246.   flag1 = 1;
  247.   while (flag1 <= count)
  248.      {
  249.      flag = 0;
  250.  
  251.      if (lintest[flag1][0]=='\0')
  252.        {
  253.        if (line[0]=='\n')
  254.          flag = 1;
  255.        }
  256.      else
  257.        {
  258.  
  259. /*  Here is the call to the actual matching routine  */
  260.        strcpy(linetst1,lintest[flag1]);
  261.        linetest();
  262.        }
  263.  
  264. /*  Quit checking if a match is found  */
  265.      if (flag != 0)
  266.         flag1 = count;
  267.      flag1++;
  268.      }
  269.  
  270. /*  No match -- output line to destination file  */
  271.   if (flag == 0)
  272.     {
  273.     out_line++;
  274.     if (fputs(inline,outfile) == EOF)
  275.        helpscrn(4);
  276.     }
  277.  
  278. /*  A match -- increment counter  */
  279.   else
  280.     {
  281.     strp_line++;
  282.     }
  283.  
  284. /*  Display current line count so user doesn't get worried  */
  285.   printf("\r %#05u   %#05u   %#05u",in_line,out_line,strp_line);
  286.   }
  287.  
  288. /*  Close the source file  */
  289. flag=fclose(infile);
  290. if (flag != 0)
  291.    helpscrn(5);
  292.  
  293. /*  Close the destination file  */
  294. flag=fclose(outfile);
  295. if (flag != 0)
  296.    helpscrn(6);
  297.  
  298. /*  Thank the user and exit gracefully  */
  299. printf("\n\nConversion complete.  Thank-you for using STRPLINE.\n\n");
  300. exit(0);
  301. }
  302.  
  303.  
  304. /*  This is the routine that actually checks for a match  */
  305. void linetest()
  306. {
  307. int j;
  308. for ( j=0; linetst1[j] == inline[j]; j++ )
  309.     if (linetst1[j+1] == '\0') flag = 1;
  310. }
  311.  
  312.  
  313. /*  Here are the error messages and VERY brief instructions  */
  314. void helpscrn(int ernum2)
  315. {
  316. printf("\n\n");
  317. switch (ernum2) {
  318.  
  319. case  2 : printf("Unable to open input file - %s\n\n",flnames[1]);
  320.           break;
  321.  
  322. case  3 : printf("Unable to open output file - %s\n\n",flnames[1]);
  323.           break;
  324.  
  325. case  4 : printf("Problem writing to output file - %s\n\n",flnames[1]);
  326.           break;
  327.  
  328. case  5 : printf("Problem closing input file - %s\n\n",flnames[1]);
  329.           break;
  330.  
  331. case  6 : printf("Problem closing output file - %s\n\n",flnames[2]);
  332.           break;
  333.  
  334. case  7 : printf("Input file not in current directory - %s\n\n",flnames[1]);
  335.           break;
  336.  
  337. case  8 : printf("Unable to open configuration file - %s\n\n",flnames[2]);
  338.           break;
  339.  
  340. case  9 : printf("Problem closing configuration file - %s\n\n",flnames[2]);
  341.           break;
  342.  
  343. case 10 : printf("No patterns in configuration file to strip.\n\n");
  344.           break;
  345.  
  346. case 11 : printf("Unable to open backup file - %s\n\n",flnames[3]);
  347.           break;
  348. }
  349.  
  350. printf("This program is used to remove selected");
  351. printf(" lines from an input text file.  The\n");
  352. printf("lines to be removed must begin with one");
  353. printf(" of a series of patterns listed in a\n");
  354. printf("configuration file.  The program is used as follows:\n\n");
  355. printf("     STRPLINE <filename> [configfile]\n\n");
  356. printf("where <filename>   is the name of the text file to be stripped\n");
  357. printf("      [configfile] is the file containing the patters to match\n");
  358. printf("                   default is STRPLINE.CFG");
  359. printf(" in the current directory\n\n");
  360. exit(ernum2);
  361. }
  362.  
  363.